﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN400001UV_Mzgh;
using YLW.Plat.Jhfw.Model.JhfwMzgh;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN400001UV_Mzgh
    {
        public static string OutPatientInfoAdd(AddJhfwMzghIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPA_IN400001UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN400001UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[]
                            {
                                new item{
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = receiver_name
                                }
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[]
                            {
                                new item
                                {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = sender_name
                                }
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "STC",
                    moodCode = "EVN",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        encounterEvent = new encounterEvent
                        {
                            classCode = "ENC",
                            moodCode = "EVN",
                            id = new id
                            {
                                item = new item[] {
                                    new item{
                                        root = "2.16.156.10011.1.11",
                                        extension = ""
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.8",
                                        extension = jsonData.series
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.9",
                                        extension = jsonData.jzlsh
                                    }
                                }
                            },
                            code = new code
                            {
                                code1 = "1",
                                codeSystem = "2.16.156.10011.2.3.1.271",
                                codeSystemName = "患者类型代码表",
                                displayName1 = new displayName1 { value = "门诊" }
                            },
                            statusCode = new statusCode(),
                            effectiveTime = new effectiveTime
                            {
                                low = new low { value = jsonData.jz_date }
                            },
                            reasonCode = new reasonCode
                            {
                                item = new item
                                {
                                    Item = new originalText
                                    {
                                        value = jsonData.diagname
                                    }
                                }
                            },
                            admissionReferralSourceCode = new admissionReferralSourceCode
                            {
                                code = jsonData.service,
                                codeSystem = "2.16.156.10011.2.3.1.248",
                                codeSystemName = "医疗保险类别代码",
                                displayName1 = new displayName1 { value = jsonData.service == "2" ? "自费" : "浙江省医保" }
                            },
                            subject = new subject
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root = "2.16.156.10011.2.5.1.4",
                                                extension = jsonData.mrn
                                            }
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.3",
                                                    extension = jsonData.pcid
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                Item = new part
                                                {
                                                    value = jsonData.pname
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            admitter = new admitter
                            {
                                typeCode = "ADM",
                                time = new time(),
                                assignedPerson = new assignedPerson
                                {
                                    classCode = "ASSIGNED",
                                    id = new id
                                    {
                                        item = new item[] {
                                            new item
                                            {
                                                root = "2.16.156.10011.1.4",
                                                extension = jsonData.in_empid
                                            }
                                        }
                                    },
                                    assignedPerson1 = new assignedPerson
                                    {
                                        classCode = "PSN",
                                        determinerCode = "INSTANCE",
                                        name = new name
                                        {
                                            item = new item
                                            {

                                            }
                                        }
                                    }
                                }
                            },
                            location = new location
                            {
                                typeCode = "LOC",
                                time = new time(),
                                statusCode = new statusCode
                                {
                                    code = "active"
                                },
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    location = new location
                                    {
                                        classCode = "PLC",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.26",
                                                    extension = jsonData.jzksdm
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                Item = new part
                                                {
                                                    value = jsonData.jzksmc
                                                }
                                            }
                                        }
                                    },
                                    serviceProviderOrganization = new serviceProviderOrganization
                                    {
                                        classCode = "ORG",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.5",
                                                    extension = ""
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                dotype = new value { value1 = jsonData.dotype },
                                sjly = new value { value1 = jsonData.sjly },
                                lyxt = new value { value1 = jsonData.lyxt },
                                hoscode = new value { value1 = jsonData.hoscode },
                                jzlsh = new value { value1 = jsonData.jzlsh },
                                mrn = new value { value1 = jsonData.mrn },
                                pname = new value { value1 = jsonData.pname },
                                pcid = new value { value1 = jsonData.pcid },
                                series = new value { value1 = jsonData.series },
                                service = new value { value1 = jsonData.service },
                                stay = new value { value1 = jsonData.stay },
                                jzksdm = new value { value1 = jsonData.jzksdm },
                                jzksmc = new value { value1 = jsonData.jzksmc },
                                jzysdm = new value { value1 = jsonData.jzysdm },
                                jzysxm = new value { value1 = jsonData.jzysxm },
                                jz_date = new value { value1 = jsonData.jz_date },
                                diagcode = new value { value1 = jsonData.diagcode },
                                diagname = new value { value1 = jsonData.diagname },
                                useflag = new value { value1 = jsonData.useflag },
                                in_empid = new value { value1 = jsonData.in_empid },
                                in_date = new value { value1 = jsonData.in_date },
                                xg_empid = new value { value1 = jsonData.xg_empid },
                                xgsj = new value { value1 = jsonData.xgsj }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
